﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineMovieTicket.Services.IServices;
using OnlineMovieTicket.Services.Services;
using OnlineMovieTicket.Model;

namespace OnlineMovieTicket.WebForms.BuyTicket
{
    public partial class SelectTicket : System.Web.UI.Page
    {
        ITransactionService transactionService = new TransactionService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int movieAssignmentId = Int32.Parse(Request.Params.Get("movieAssignId"));
                this.movieAssignmentId.Value = movieAssignmentId.ToString();
                
                List<TransactionDetail> transModel = transactionService.getMovieBookingDetail(movieAssignmentId);
                cleanAllBtn();
                updateBtn(transModel);

                Model.TransactionDetail saveTransModel = transactionService.saveTransaction(movieAssignmentId);
                int transactionNum = saveTransModel.TransactionID;
                this.transNum.Value = transactionNum.ToString();

                TransactionDetail movieDetail = transactionService.getMovieDetail(movieAssignmentId);
                this.lblMovieName.Text = movieDetail.movieName;
                this.lblTime.Text = movieDetail.ShowDateTime.TimeOfDay.ToString();
            }
        }

        protected void Timer_Tick(object sender, EventArgs e)
        {
            cleanAllBtn();
            int movieAssignmentId = Int32.Parse(this.movieAssignmentId.Value);
            List<TransactionDetail> transModel = transactionService.getMovieBookingDetail(movieAssignmentId);
            TransactionDetail movieDetail = transactionService.getMovieDetail(movieAssignmentId);
            this.lblMovieName.Text = movieDetail.movieName;
            this.lblTime.Text = movieDetail.ShowDateTime.TimeOfDay.ToString();
            updateBtn(transModel);
        }

        

        private void cleanAllBtn()
        {
            this.A1.Enabled = true;
            this.A1.BackColor = System.Drawing.Color.LightBlue;
            this.A2.Enabled = true;
            this.A2.BackColor = System.Drawing.Color.LightBlue;
            this.A3.Enabled = true;
            this.A3.BackColor = System.Drawing.Color.LightBlue;
            this.A4.Enabled = true;
            this.A4.BackColor = System.Drawing.Color.LightBlue;
            this.B1.Enabled = true;
            this.B1.BackColor = System.Drawing.Color.LightBlue;
            this.B2.Enabled = true;
            this.B2.BackColor = System.Drawing.Color.LightBlue;
            this.B3.Enabled = true;
            this.B3.BackColor = System.Drawing.Color.LightBlue;
            this.B4.Enabled = true;
            this.B4.BackColor = System.Drawing.Color.LightBlue;
            this.C1.Enabled = true;
            this.C1.BackColor = System.Drawing.Color.LightBlue;
            this.C2.Enabled = true;
            this.C2.BackColor = System.Drawing.Color.LightBlue;
            this.C3.Enabled = true;
            this.C3.BackColor = System.Drawing.Color.LightBlue;
            this.C4.Enabled = true;
            this.C4.BackColor = System.Drawing.Color.LightBlue;
        }

        private void updateBtn(List<TransactionDetail> transModel)
        {
            String movieName = "";
            String transactionId = this.transNum.Value;

            foreach (TransactionDetail transDetail in transModel)
            {
                String status = transDetail.Status;
                movieName = transDetail.movieName;
                foreach (String seatNum in transDetail.SeatNum)
                {
                    if (status.Equals("A"))
                    {
                        if (seatNum.Equals("A1"))
                        {
                            this.A1.Enabled = false;
                            this.A1.BackColor = System.Drawing.Color.AliceBlue;
                        }
                        else if (seatNum.Equals("A2"))
                        {
                            this.A2.Enabled = false;
                            this.A2.BackColor = System.Drawing.Color.AliceBlue;
                        }
                        else if (seatNum.Equals("A3"))
                        {
                            this.A3.Enabled = false;
                            this.A3.BackColor = System.Drawing.Color.AliceBlue;
                        }
                        else if (seatNum.Equals("A4"))
                        {
                            this.A4.Enabled = false;
                            this.A4.BackColor = System.Drawing.Color.AliceBlue;
                        }
                        else if (seatNum.Equals("B1"))
                        {
                            this.B1.Enabled = false;
                            this.B1.BackColor = System.Drawing.Color.AliceBlue;
                        }
                        else if (seatNum.Equals("B2"))
                        {
                            this.B2.Enabled = false;
                            this.B2.BackColor = System.Drawing.Color.AliceBlue;
                        }
                        else if (seatNum.Equals("B3"))
                        {
                            this.B3.Enabled = false;
                            this.B3.BackColor = System.Drawing.Color.AliceBlue;
                        }
                        else if (seatNum.Equals("B4"))
                        {
                            this.B4.Enabled = false;
                            this.B4.BackColor = System.Drawing.Color.AliceBlue;
                        }
                        else if (seatNum.Equals("C1"))
                        {
                            this.C1.Enabled = false;
                            this.C1.BackColor = System.Drawing.Color.AliceBlue;
                        }
                        else if (seatNum.Equals("C2"))
                        {
                            this.C2.Enabled = false;
                            this.C2.BackColor = System.Drawing.Color.AliceBlue;
                        }
                        else if (seatNum.Equals("C3"))
                        {
                            this.C3.Enabled = false;
                            this.C3.BackColor = System.Drawing.Color.AliceBlue;
                        }
                        else if (seatNum.Equals("C4"))
                        {
                            this.C4.Enabled = false;
                            this.C4.BackColor = System.Drawing.Color.AliceBlue;
                        }
                    }
                    else if (status.Equals("P"))
                    {
                        if (seatNum.Equals("A1"))
                        {
                            if (!transDetail.TransactionID.ToString().Equals(transactionId))
                            {
                                this.A1.Enabled = false;
                            }
                            this.A1.BackColor = System.Drawing.Color.Cyan;
                        }
                        else if (seatNum.Equals("A2"))
                        {
                            if (!transDetail.TransactionID.ToString().Equals(transactionId))
                            {
                                this.A2.Enabled = false;
                            }
                            this.A2.BackColor = System.Drawing.Color.Cyan;
                        }
                        else if (seatNum.Equals("A3"))
                        {
                            if (!transDetail.TransactionID.ToString().Equals(transactionId))
                            {
                                this.A3.Enabled = false;
                            }
                            this.A3.BackColor = System.Drawing.Color.Cyan;
                        }
                        else if (seatNum.Equals("A4"))
                        {
                            if (!transDetail.TransactionID.ToString().Equals(transactionId))
                            {
                                this.A4.Enabled = false;
                            }
                            this.A4.BackColor = System.Drawing.Color.Cyan;
                        }
                        else if (seatNum.Equals("B1"))
                        {
                            if (!transDetail.TransactionID.ToString().Equals(transactionId))
                            {
                                this.B1.Enabled = false;
                            }
                            this.B1.BackColor = System.Drawing.Color.Cyan;
                        }
                        else if (seatNum.Equals("B2"))
                        {
                            if (!transDetail.TransactionID.ToString().Equals(transactionId))
                            {
                                this.B2.Enabled = false;
                            }
                            this.B2.BackColor = System.Drawing.Color.Cyan;
                        }
                        else if (seatNum.Equals("B3"))
                        {
                            if (!transDetail.TransactionID.ToString().Equals(transactionId))
                            {
                                this.B3.Enabled = false;
                            }
                            this.B3.BackColor = System.Drawing.Color.Cyan;
                        }
                        else if (seatNum.Equals("B4"))
                        {
                            if (!transDetail.TransactionID.ToString().Equals(transactionId))
                            {
                                this.B4.Enabled = false;
                            }
                            this.B4.BackColor = System.Drawing.Color.Cyan;
                        }
                        else if (seatNum.Equals("C1"))
                        {
                            if (!transDetail.TransactionID.ToString().Equals(transactionId))
                            {
                                this.C1.Enabled = false;
                            }
                            this.C1.BackColor = System.Drawing.Color.Cyan;
                        }
                        else if (seatNum.Equals("C2"))
                        {
                            if (!transDetail.TransactionID.ToString().Equals(transactionId))
                            {
                                this.C2.Enabled = false;
                            }
                            this.C2.BackColor = System.Drawing.Color.Cyan;
                        }
                        else if (seatNum.Equals("C3"))
                        {
                            if (!transDetail.TransactionID.ToString().Equals(transactionId))
                            {
                                this.C3.Enabled = false;
                            }
                            this.C3.BackColor = System.Drawing.Color.Cyan;
                        }
                        else if (seatNum.Equals("C4"))
                        {
                            if (!transDetail.TransactionID.ToString().Equals(transactionId))
                            {
                                this.C4.Enabled = false;
                            }
                            this.C4.BackColor = System.Drawing.Color.Cyan;
                        }
                    }
                }
            }
        }



        protected void A1_Click(object sender, EventArgs e)
        {
            
            int transID = -1;
            Model.TransactionDetail transModel = new TransactionDetail();

            if(this.transNum.Value !=null)
            {
                transID = Int32.Parse(this.transNum.Value);
            }
            if (this.A1.BackColor.Equals(System.Drawing.Color.Cyan))
            {

                transModel = transactionService.saveTransaction(transID, "A1", "P", Int32.Parse(movieAssignmentId.Value));
                this.A1.BackColor = System.Drawing.Color.LightBlue;


            }
            else
            {

                transModel = transactionService.saveTransaction(transID, "A1", "", Int32.Parse(movieAssignmentId.Value));
                this.A1.BackColor = System.Drawing.Color.Cyan;

            }
            this.lblAmt.Text = transModel.Amount.ToString();
            this.transNum.Value = transModel.TransactionID.ToString();
            
        }

        protected void A2_Click(object sender, EventArgs e)
        {
            int transID = -1;
            Model.TransactionDetail transModel = new TransactionDetail();
            if (this.transNum.Value != null)
            {
                transID = Int32.Parse(this.transNum.Value);
            }
            if (this.A2.BackColor.Equals(System.Drawing.Color.Cyan))
            {

                transModel = transactionService.saveTransaction(transID, "A2", "P", Int32.Parse(movieAssignmentId.Value));
                this.B1.BackColor = System.Drawing.Color.LightBlue;

            }
            else
            {

                transModel = transactionService.saveTransaction(transID, "A2", "", Int32.Parse(movieAssignmentId.Value));
                this.A2.BackColor = System.Drawing.Color.Cyan;

             
            }
            this.lblAmt.Text = transModel.Amount.ToString();
            this.transNum.Value = transModel.TransactionID.ToString();
        }

        protected void A3_Click(object sender, EventArgs e)
        {
            int transID = -1;
            Model.TransactionDetail transModel = new TransactionDetail();
            if (this.transNum.Value != null)
            {
                transID = Int32.Parse(this.transNum.Value);
            }
            if (this.A3.BackColor.Equals(System.Drawing.Color.Cyan))
            {

                transModel = transactionService.saveTransaction(transID, "A3", "P", Int32.Parse(movieAssignmentId.Value));
                this.A3.BackColor = System.Drawing.Color.LightBlue;

            }
            else
            {

                transModel = transactionService.saveTransaction(transID, "A3", "", Int32.Parse(movieAssignmentId.Value));
                this.A3.BackColor = System.Drawing.Color.Cyan;


            }
            this.lblAmt.Text = transModel.Amount.ToString();
            this.transNum.Value = transModel.TransactionID.ToString();
        }

        protected void A4_Click(object sender, EventArgs e)
        {
            int transID = -1;
            Model.TransactionDetail transModel = new TransactionDetail();
            if (this.transNum.Value != null)
            {
                transID = Int32.Parse(this.transNum.Value);
            }
            if (this.A4.BackColor.Equals(System.Drawing.Color.Cyan))
            {

                transModel = transactionService.saveTransaction(transID, "A4", "P", Int32.Parse(movieAssignmentId.Value));
                this.A4.BackColor = System.Drawing.Color.LightBlue;


            }
            else
            {

                transModel = transactionService.saveTransaction(transID, "A4", "", Int32.Parse(movieAssignmentId.Value));
                this.A4.BackColor = System.Drawing.Color.Cyan;

            }
            this.lblAmt.Text = transModel.Amount.ToString();
            this.transNum.Value = transModel.TransactionID.ToString();
        }

        protected void B1_Click(object sender, EventArgs e)
        {
            int transID = -1;
            Model.TransactionDetail transModel = new TransactionDetail();
            if (this.transNum.Value != null)
            {
                transID = Int32.Parse(this.transNum.Value);
            }
            if (this.B1.BackColor.Equals(System.Drawing.Color.Cyan))
            {

                transModel = transactionService.saveTransaction(transID, "B1", "P", Int32.Parse(movieAssignmentId.Value));
                this.B1.BackColor = System.Drawing.Color.LightBlue;

            }
            else
            {

                transModel = transactionService.saveTransaction(transID, "B1", "", Int32.Parse(movieAssignmentId.Value));
                this.B1.BackColor = System.Drawing.Color.Cyan;

            }
            this.lblAmt.Text = transModel.Amount.ToString();
            this.transNum.Value = transModel.TransactionID.ToString();
        }

        protected void B2_Click(object sender, EventArgs e)
        {
            int transID = -1;
            Model.TransactionDetail transModel = new TransactionDetail();
            if (this.transNum.Value != null)
            {
                transID = Int32.Parse(this.transNum.Value);
            }
            if (this.B2.BackColor.Equals(System.Drawing.Color.Cyan))
            {

                transModel = transactionService.saveTransaction(transID, "B2", "P", Int32.Parse(movieAssignmentId.Value));
                this.B2.BackColor = System.Drawing.Color.LightBlue;

            }
            else
            {

                transModel = transactionService.saveTransaction(transID, "B2", "", Int32.Parse(movieAssignmentId.Value));
                this.B2.BackColor = System.Drawing.Color.Cyan;

            }
            this.lblAmt.Text = transModel.Amount.ToString();
            this.transNum.Value = transModel.TransactionID.ToString();
        }

        protected void B3_Click(object sender, EventArgs e)
        {
            int transID = -1;
            Model.TransactionDetail transModel = new TransactionDetail();
            if (this.transNum.Value != null)
            {
                transID = Int32.Parse(this.transNum.Value);
            }
            if (this.B3.BackColor.Equals(System.Drawing.Color.Cyan))
            {

                transModel = transactionService.saveTransaction(transID, "B3", "P", Int32.Parse(movieAssignmentId.Value));
                this.B3.BackColor = System.Drawing.Color.LightBlue;


            }
            else
            {

                transModel = transactionService.saveTransaction(transID, "B3", "", Int32.Parse(movieAssignmentId.Value));
                this.B3.BackColor = System.Drawing.Color.Cyan;

               

            }
            this.lblAmt.Text = transModel.Amount.ToString();
            this.transNum.Value = transModel.TransactionID.ToString();
        }

        protected void B4_Click(object sender, EventArgs e)
        {
            int transID = -1;
            Model.TransactionDetail transModel = new TransactionDetail();
            if (this.transNum.Value != null)
            {
                transID = Int32.Parse(this.transNum.Value);
            }
            if (this.B4.BackColor.Equals(System.Drawing.Color.Cyan))
            {

                transModel = transactionService.saveTransaction(transID, "B4", "P", Int32.Parse(movieAssignmentId.Value));
                this.B4.BackColor = System.Drawing.Color.LightBlue;

            }
            else
            {

                transModel = transactionService.saveTransaction(transID, "B4", "", Int32.Parse(movieAssignmentId.Value));
                this.B4.BackColor = System.Drawing.Color.Cyan;


            }
            this.lblAmt.Text = transModel.Amount.ToString();
            this.transNum.Value = transModel.TransactionID.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int transId = Int32.Parse(this.transNum.Value);

            if (transId != -1)
            {
                transactionService.updateTransactionTime(transId);
                
            }
            String transNumStr = this.transNum.Value;
            String movieAssigId =this.movieAssignmentId.Value;

            
            Response.Redirect("MakePurchase.aspx?transactionID=" + transNumStr + "&movieAssignId=" + movieAssigId);

        }

        protected void C4_Click(object sender, EventArgs e)
        {
            int transID = -1;
            Model.TransactionDetail transModel = new TransactionDetail();
            if (this.transNum.Value != null)
            {
                transID = Int32.Parse(this.transNum.Value);
            }
            if (this.C4.BackColor.Equals(System.Drawing.Color.Cyan))
            {

                transModel = transactionService.saveTransaction(transID, "C4", "P", Int32.Parse(movieAssignmentId.Value));
                this.C4.BackColor = System.Drawing.Color.LightBlue;


            }
            else
            {

                transModel = transactionService.saveTransaction(transID, "C4", "", Int32.Parse(movieAssignmentId.Value));
                this.C4.BackColor = System.Drawing.Color.Cyan;

               

            }
            this.lblAmt.Text = transModel.Amount.ToString();
            this.transNum.Value = transModel.TransactionID.ToString();
        }

        protected void C1_Click(object sender, EventArgs e)
        {
            int transID = -1;
            Model.TransactionDetail transModel = new TransactionDetail();
            if (this.transNum.Value != null)
            {
                transID = Int32.Parse(this.transNum.Value);
            }
            if (this.C1.BackColor.Equals(System.Drawing.Color.Cyan))
            {

                transModel = transactionService.saveTransaction(transID, "C1", "P", Int32.Parse(movieAssignmentId.Value));
                this.C1.BackColor = System.Drawing.Color.LightBlue;


            }
            else
            {

                transModel = transactionService.saveTransaction(transID, "C1", "", Int32.Parse(movieAssignmentId.Value));
                this.C4.BackColor = System.Drawing.Color.Cyan;

                

            }
            this.lblAmt.Text = transModel.Amount.ToString();
            this.transNum.Value = transModel.TransactionID.ToString();
        }

        protected void C2_Click(object sender, EventArgs e)
        {
            int transID = -1;
            Model.TransactionDetail transModel = new TransactionDetail();
            if (this.transNum.Value != null)
            {
                transID = Int32.Parse(this.transNum.Value);
            }
            if (this.C2.BackColor.Equals(System.Drawing.Color.Cyan))
            {

                transModel = transactionService.saveTransaction(transID, "C2", "P", Int32.Parse(movieAssignmentId.Value));
                this.C2.BackColor = System.Drawing.Color.LightBlue;


            }
            else
            {

                transModel = transactionService.saveTransaction(transID, "C2", "", Int32.Parse(movieAssignmentId.Value));
                this.C2.BackColor = System.Drawing.Color.Cyan;

                

            }
            this.lblAmt.Text = transModel.Amount.ToString();
            this.transNum.Value = transModel.TransactionID.ToString();
        }

        protected void C3_Click(object sender, EventArgs e)
        {
            int transID = -1;
            Model.TransactionDetail transModel = new TransactionDetail();
            if (this.transNum.Value != null)
            {
                transID = Int32.Parse(this.transNum.Value);
            }
            if (this.C3.BackColor.Equals(System.Drawing.Color.Cyan))
            {

                transModel = transactionService.saveTransaction(transID, "C3", "P", Int32.Parse(movieAssignmentId.Value));
                this.C3.BackColor = System.Drawing.Color.LightBlue;


            }
            else
            {

                transModel = transactionService.saveTransaction(transID, "C3", "", Int32.Parse(movieAssignmentId.Value));
                this.C3.BackColor = System.Drawing.Color.Cyan;

                

            }
            this.lblAmt.Text = transModel.Amount.ToString();
            this.transNum.Value = transModel.TransactionID.ToString();
        }


    }
}